package com.example.qxapp.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.qxapp.Bean.Comunity;
import com.example.qxapp.Bean.User;
import com.example.qxapp.R;
import com.example.qxapp.activity.MycomunityActivity;
import com.example.qxapp.adapter.Fmycomadapter;
import com.example.qxapp.adapter.mycomadapter;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Framentcom extends Fragment {
    private SwipeRefreshLayout swipe_mycom1;
    private RecyclerView rv_mycom1;
    private TextView error_mycom1;

    private List<Comunity> data;
    Fmycomadapter fmycomadapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mycom,container,false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        refresh();
        swipe_mycom1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        User user = BmobUser.getCurrentUser(User.class);
        BmobQuery<Comunity> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("user",user);
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<Comunity>() {
            @Override
            public void done(List<Comunity> list, BmobException e) {
                swipe_mycom1.setRefreshing(false);
                if (e==null){
                    data = list;
                    if (data.size()>0){
                        swipe_mycom1.setVisibility(View.VISIBLE);
                        fmycomadapter = new Fmycomadapter(getActivity(),data);
                        rv_mycom1.setLayoutManager(new LinearLayoutManager(getActivity()));
                        rv_mycom1.setAdapter(fmycomadapter);

                    }else {
                        error_mycom1.setVisibility(View.VISIBLE);
                    }}
//                }else {
//                    Toast.makeText(MycomunityActivity.this, "加载失败"+e, Toast.LENGTH_SHORT).show();
//                }
            }
        });
    }

    private void init() {
        swipe_mycom1=getActivity().findViewById(R.id.swipe_mycom1);
        rv_mycom1=getActivity().findViewById(R.id.rv_mycom1);
        error_mycom1=getActivity().findViewById(R.id.error_mycom1);
    }
}
